import argparse
import sys

import pygame

pygame.init()
pygame.mixer.init()
pygame.event.get()  # 清除所有事件队列
pygame.event.post(pygame.event.Event(101, info={'set': 'chinese'}))  # 设置语言环境为中文

from common import get_current_level


def main(levels: list):
    # 游戏主循环
    current_level = 0
    level_module = levels[current_level]
    screen = pygame.display.set_mode(
        (level_module.screen_width, level_module.screen_height)
    )
    level_module.draw(screen)
    running = True
    while running:
        # 获取当前关卡
        next_level = get_current_level()
        if next_level > len(levels) - 1:
            running = False
            break

        if next_level != current_level:
            level_module = levels[next_level]
            screen = pygame.display.set_mode(
                (level_module.screen_width, level_module.screen_height)
            )
            current_level = next_level

        # 绘制当前关卡
        level_module.draw(screen)

        # 更新屏幕
        pygame.display.flip()

    # 退出 Pygame
    pygame.quit()
    sys.exit()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("game", help="The game to run", choices=["1", "2", "3", "4", "5", "6"])
    args = parser.parse_args()

    if args.game == "1":
        from game_1 import levels
    elif args.game == "2":
        from game_2 import levels
    elif args.game == "3":
        from game_3 import levels
    elif args.game == "4":
        from game_4 import levels
    elif args.game == "5":
        from game_5 import levels
    elif args.game == "6":
        from game_6 import levels
    else:
        raise ValueError("Invalid game")

    main(levels)
